﻿namespace JunFan.Tests.Infrastructure
{
    using System;
    using System.Data.Entity;
    using Mapping;
    using Repositories;

    public static class TestSetup
    {
        public static void SetupDatabase(bool destroyAndCreateNew)
        {
            IDatabaseInitializer<EfContext> strategy;

            if (destroyAndCreateNew)
            {
                strategy = new DropCreateDatabaseAlways<EfContext>();
            }
            else
            {
                strategy = new DropCreateDatabaseIfModelChanges<EfContext>();
            }

            Database.SetInitializer(strategy);

            // Set |DataDirectory| value
            AppDomain.CurrentDomain.SetData("DataDirectory",
                                            String.Format(@"{0}\App_Data\", AppDomain.CurrentDomain.BaseDirectory));

            CleanupDatabase();
        }

        public static void CleanupDatabase()
        {
            using (var efContext = new EfContext())
            {
                efContext.Database.ExecuteSqlCommand("TRUNCATE TABLE ITEMS");
            }
        }

        public static void InitializeMappings()
        {
            AutoMapperInitializer.SetupAutoMapper();
        }
    }
}